home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3794 / 3794.xpi / chrome / content / facebook.js < prev    next >
Text File  |  2009-08-13  |  3KB  |  87 lines

  1. /**
  2.  *
  3.  * The source code included in this file is licensed to you by Facebook under
  4.  * the Apache License, Version 2.0.  Accordingly, the following notice
  5.  * applies to the source code included in this file:
  6.  *
  7.  * Copyright ┬⌐ 2009 Facebook, Inc.
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10.  * not use this file except in compliance with the License. You may obtain
  11.  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16.  * License for the specific language governing permissions and limitations
  17.  * under the License.
  18.  *
  19.  */
  20.  
  21.  
  22. // Note that this file is intended for login-related API function calls only
  23. // (ie facebook.auth.*).  Other API calls go through the Facebook xpcom service.
  24. var Cc = Components.classes;
  25. var Ci = Components.interfaces;
  26.  
  27. // Load MD5 code...
  28. Cc['@mozilla.org/moz/jssubscript-loader;1']
  29.     .getService(Ci.mozIJSSubScriptLoader)
  30.     .loadSubScript('chrome://facebook/content/md5.js');
  31.  
  32. function FacebookLoginClient() {
  33.     this.fbSvc = Cc['@facebook.com/facebook-service;1'].getService().QueryInterface(Ci.fbIFacebookService);
  34. }
  35.  
  36. FacebookLoginClient.prototype = {
  37.     findNamespace: /xmlns=(?:"[^"]*"|'[^']*')/,
  38.     generateSig: function (params) {
  39.         var str = '';
  40.         params.sort();
  41.         for (var i = 0; i < params.length; i++) {
  42.             str += params[i];
  43.         }
  44.         str += this.fbSvc.secret;
  45.         return MD5(str);
  46.     },
  47.     callMethod: function (method, params, callback) {
  48.         params.push('method=' + method);
  49.         params.push('api_key=' + this.fbSvc.apiKey);
  50.         params.push('v=1.0');
  51.         params.push('sig=' + this.generateSig(params));
  52.         var req = new XMLHttpRequest();
  53.         var ns_re = this.findNamespace;
  54.         req.onreadystatechange = function (event) {
  55.             if (req.readyState == 4) {
  56.                 var status;
  57.                 try {
  58.                     status = req.status;
  59.                 } catch (e) {
  60.                     status = 0;
  61.                 }
  62.  
  63.                 if (status == 200) {
  64.                     dump( 'login:' + req.responseText.indexOf("\n") + "\n" );
  65.                     req.text = req.responseText.substr(req.responseText.indexOf("\n"));
  66.                     var ns = req.text.match(ns_re);
  67.                     if (ns)
  68.                       default xml namespace = ns;
  69.                     req.xmldata = new XML(req.text);
  70.                     callback(req);
  71.                 }
  72.             }
  73.         };
  74.         try {
  75.             var restserver = 'https://api.facebook.com/restserver.php';
  76.             req.open('POST', restserver, true);
  77.             req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  78.             req.send(params.join('&'));
  79.             dump( params.join('&') + "\n" );
  80.         } catch (e) {
  81.             dump('Exception sending REST request: ' + e + '\n');
  82.         }
  83.     }
  84. };
  85.  
  86. dump('loaded facebook.js\n');
  87.